package com.jizhang.service.impl;

import com.jizhang.base.model.BaseModel;
import com.jizhang.base.model.BusinessException;
import com.jizhang.entity.User;
import com.jizhang.mapper.UserMapper;
import com.jizhang.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭博民
 * @since 2022-07-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    // 根据账号查询用户
    public BaseModel findUser(String username, BaseModel baseModel) throws Exception {
        User user=userMapper.findUser(username);
        System.out.println("查询到的用户数据："+user);
        if (user==null){
            System.out.println("用户为空");
            baseModel.setCode(1001);
            baseModel.setMessage("用户数据为空,可以注册");
            return baseModel;
        }
        baseModel.setCode(0);
        baseModel.setData(user);
        return baseModel;
    }

    // 注册用户 添加用户
    public BaseModel addUser(User user, BaseModel baseModel) throws Exception {
        // 先对用户进行查询,为空才能添加,避免重复
        User result=userMapper.findUser(user.getUsername());
        if (result==null) {
            System.out.println("用户为空,可以进行添加操作");
            // 执行添加用户
            int num =userMapper.addUser(user);
            if (num<=0){
                // 二者用一   throw new BusinessException(1002, "添加题目失败");
                baseModel.setCode(1002);
                baseModel.setMessage("用户添加失败");
                return baseModel;
            }
            // 添加用户成功!
            baseModel.setCode(0);
            baseModel.setMessage("添加(注册)用户成功");
            return baseModel;
        }
        // 注册失败,已存在
        baseModel.setCode(1003);
        baseModel.setMessage("用户已存在,请重新输入");
        return baseModel;
    }

    // 修改用户
    public BaseModel updateUser(User user, BaseModel baseModel) throws Exception {
        int result=userMapper.updateUser(user);
        if (result <= 0) {
            throw new BusinessException(1004, "修改用户信息失败");
        }
        baseModel.setCode(0);
        baseModel.setMessage("更新用户信息成功");
        return baseModel;
    }

    // 用户登录
    public BaseModel loginUser(User user, BaseModel baseModel) throws Exception {
        User result=userMapper.findUser(user.getUsername());
        System.out.println("查询到的用户数据："+result);
        if (result==null){
            System.out.println("用户为空");
            baseModel.setCode(1005);
            baseModel.setMessage("用户账号不存在");
            return baseModel;
        }
        // 当账号存在时,执行登录,去验证密码
        User login=userMapper.loginUser(user);
        System.out.println("返回值"+login);
        if (login==null){
            System.out.println("密码错误");
            baseModel.setCode(1006);
            baseModel.setMessage("用户账号或密码错误");
            return baseModel;
        }
        baseModel.setCode(0);
        baseModel.setMessage("用户登录成功");
        baseModel.setData(login);
        return baseModel;
    }
}
